package com.example.yrdev.ordering_system;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.dhF9.dhF9Jni;
import com.example.yrdev.ordering_system.Common.CommonActivity;
import com.example.yrdev.ordering_system.Common.Setting;
import com.example.yrdev.ordering_system.User_Login_Register.LoginActivity;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by luke on 01/12/2016.
 */

public class RegisAppActivity extends CommonActivity {
    @InjectView(R.id.regisCode)EditText regisCode;
    @InjectView(R.id.devNo)TextView devNo;
    private final String regisKey="regis";
    private String correpondCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //initLanguage(GlobalFunction.getCurrentLanKey(this));

        GlobalFunction.context=this;
        setContentView(R.layout.regis_app_layout);
        ButterKnife.inject(this);
        initView();



        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);//直接弹出输入框\
        String temp= dhF9Jni.calculateRegisCode(devNo.getText().toString());
        String regtxt=getRegisStat().toLowerCase();
        if (regtxt.equals(temp.toLowerCase())){
            gotoActivity(LoginActivity.class,0);
        }
    }


    private void setClipboard(String text) {
        if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {

            android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.setText(text);
        } else {
            android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", text);
            clipboard.setPrimaryClip(clip);
        }
        GlobalFunction.ToastAlert(this,"复制成功");
    }


    public  void saveRegisStat(){

      //  SharedPreferences preferences=getSharedPreferences("regis", Context.MODE_PRIVATE);
       // SharedPreferences.Editor editor=preferences.edit();
//        editor.putInt(regisKey,1).apply();

     //   editor.putString(regisKey,regisCode.getText().toString());
        Setting.getInstance().setRegiscode(regisCode.getText().toString());
        Setting.getInstance().saveSetting();

    }
    private String getRegisStat(){
     //   SharedPreferences preferences=getSharedPreferences("regis", Context.MODE_PRIVATE);

      //  return   preferences.getString(regisKey,"");
        //devID=preferences.getString(devIdKey,"1");
        Setting.getInstance().init();
        return Setting.getInstance().getRegiscode();

    }

    @Override
    public void initView() {
        super.initView();
        getSupportActionBar().hide();

        String temp= GlobalFunction.getSerialNumber();
        GlobalFunction.printTextByTag(temp);
        devNo.setText(temp);
        correpondCode=  dhF9Jni.calculateRegisCode(temp);

        devNo.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                GlobalFunction.ToastAlert(RegisAppActivity.this,"复制成功");
                setClipboard(devNo.getText().toString());
                return false;
            }
        });

        regisCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                regisCode.setCursorVisible(true);
            }
        });
        regisCode.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId== EditorInfo.IME_ACTION_DONE){
                    regisCode.setCursorVisible(false);
                }
                return false;
            }
        });


    }

    public void checkRegis(View view){
        if (regisCode.getText().toString().toLowerCase().equals(correpondCode.toLowerCase())){
            saveRegisStat();
            gotoActivity(LoginActivity.class,0);
            GlobalFunction.ToastAlert(this,"注册成功");

        }else {
            GlobalFunction.ToastAlert(this,"注册码错");
        }
    }
}
